package com.zr.domain.sp.test;

import java.util.Arrays;

public class Solution {
    /*
        问题描述：
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

输入：l1 = [1,2,4], l2 = [1,3,4]
输出：[1,1,2,3,4,4]

输入：l1 = [], l2 = []
输出：[]

输入：l1 = [], l2 = [0]
输出：[0]

 */

    public static int[] mergeArray(int[] arr1,int[] arr2){
        int len1 = arr1.length;
        int len2 = arr2.length;

        int len = arr1.length + arr2.length;
        int[] res = new int[len];
        int k = 0;
        int j = 0;
        for (int i = 0; i < len; i++) {
            if (k>=len1){
                res[i] = arr2[j++];
                continue;
            }
            if (j>=len2){
                res[i] = arr1[k++];
                continue;
            }
            if (arr1[k]<arr2[j]){
                res[i] = arr1[k];
                k++;
            }else {
                res[i] = arr2[j];
                j++;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] l1 = {1,2,4}, l2 = {1,3,4};
        int[] ints = mergeArray(l1, l2);
        System.out.println(Arrays.toString(ints));
    }
}
